package cn.medemede.dao;


import cn.medemede.dbc.DatabaseConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RegionDao {
    public static String select(String parentId) throws SQLException {
        Connection connection= DatabaseConnection.getConnection();
        String SelectSQL = "SELECT REGION_NAME FROM region WHERE PARENT_ID=?";
        PreparedStatement preparedStatement=connection.prepareStatement(SelectSQL);
        preparedStatement.setString(1,parentId);
        ResultSet rs=preparedStatement.executeQuery();
        StringBuilder proviceStr= new StringBuilder();
        while (rs.next()){
            proviceStr.append(rs.getString("REGION_NAME")).append(",");
        }
        connection.close();
        preparedStatement.close();
        return proviceStr.toString();
    }

    public static String selectByCity(String city,String parentId) throws SQLException {
        Connection connection= DatabaseConnection.getConnection();
        String SelectSQL = "SELECT REGION_NAME FROM region WHERE PARENT_ID=(SELECT REGION_ID FROM region WHERE REGION_NAME=? AND PARENT_ID=?)";
        PreparedStatement preparedStatement=connection.prepareStatement(SelectSQL);
        preparedStatement.setString(1,city);
        preparedStatement.setString(2,parentId);
        ResultSet rs=preparedStatement.executeQuery();
        StringBuilder proviceStr= new StringBuilder();
        while (rs.next()){
            proviceStr.append(rs.getString("REGION_NAME")).append(",");
        }
        connection.close();
        preparedStatement.close();
        return proviceStr.toString();
    }
}
